/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package databaseupgrade;

import TimeRecording.Upgrade.SqlCommandStatusType;
import TimeRecording.Upgrade.SqlCommandType;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author david
 */
public class SqlCommandTableModel extends DefaultTableModel {

    private final static String[] HEADINGS =
            new String[] { "Label", "Description", "Status" };

    private List<SqlCommandType> commands;

    public SqlCommandTableModel(List<SqlCommandType> commands) {

        this.commands = commands;

        if (commands != null) {
            int step = 1;
            for (SqlCommandType command : commands) {
                command.setStatus(SqlCommandStatusType.NOT_YET_RUN);
                command.setLabel(command.getLabel() + "." + step++);
            }
        }
    }

    @Override
    public int getColumnCount() {
        return HEADINGS.length;
    }

    @Override
    public String getColumnName(int column) {
        return HEADINGS[column];
    }

    @Override
    public int getRowCount() {
        return (commands == null) ? 0 : commands.size();
    }

    public void refresh() {
        fireTableDataChanged();
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }

    @Override
    public Object getValueAt(int row, int column) {
        SqlCommandType command = commands.get(row);
        switch (column) {
            case 0:
                return command.getLabel();
            case 1:
                return command.getDescription();
            case 2:
                return command.getStatus();
            default:
                return null;
        }
    }

    public SqlCommandType getSelectedCommand(int row) {
        return commands.get(row);
    }

    public List<SqlCommandType> getCommands() {
        return commands;
    }
}
